package com.zy.Conterller;

import com.zy.pojo.Purchase;
import com.zy.service.PurchaseService;
import com.zy.vo.ManagementVo;
import com.zy.vo.ResultVo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sm/purchase/")
public class PurchaseCont {
    @Resource
    private PurchaseService purchaseService;
    @GetMapping("list/{pageSize}/{pageNo}")
    @RequiresPermissions("purchaser:list")
    public ResultVo getPurList(@PathVariable Integer pageSize, @PathVariable Integer pageNo, ManagementVo managementVo){

            return ResultVo.success(purchaseService.getPurList(pageNo,pageSize,managementVo));

    }

    @PostMapping("add")
    @RequiresPermissions("purchaser:add")
    public ResultVo getAdd(@RequestBody Purchase purchase){
        try {
            purchase.setCgLingcode((int)((Math.random()*9+1)*100000));
            purchase.setCgOrder(0);
            purchaseService.add(purchase);
            return ResultVo.success("采购合同添加成功");
        }catch (Exception e){
            return ResultVo.fail("采购合同添加失败",e);
        }
    }

    @PostMapping("/update")
    @RequiresPermissions("purchaser:update")//判断当前登录人有没有此权限
    public ResultVo getUpdate(@RequestBody Purchase purchase){
        try{
            purchaseService.update(purchase);
            return ResultVo.success("采购合同修改成功");
        }catch (Exception e){
            return ResultVo.fail("采购合同修改失败");
        }
    }
    @GetMapping("del/{id}")
    /*@RequiresPermissions("purchaser:del")*/
    public ResultVo del(@PathVariable String id){
        try{
            purchaseService.del(Integer.parseInt(id));
            return ResultVo.success("采购合同删除成功");
        }catch (Exception e){
            return ResultVo.fail("采购合同删除失败");
        }
    }
    @GetMapping("err/{id}")  //跟{id}进行绑定
    @RequiresPermissions("purchaser:err")
    public ResultVo geterr(@PathVariable String id){
        try{
            purchaseService.select(Integer.parseInt(id));
            return ResultVo.success("查看采购合同成功");
        }catch (Exception e){
            return ResultVo.fail("查看采购合同失败");
        }
    }
    @PostMapping("mxadd/{cgLingcode}/{goodsName}/{Lingroot}/{djia}/{dshu}/{zmoney}/{id}")
    public ResultVo mxAdd(@PathVariable Integer cgLingcode, @PathVariable String goodsName,@PathVariable("Lingroot")Integer Lingroot,@PathVariable Integer djia, @PathVariable Integer dshu,@PathVariable Integer zmoney,@PathVariable Integer id,@RequestBody Purchase purchase){
        try{
            System.out.println("------------"+id);
            System.out.println("------------"+cgLingcode);
            System.out.println("------------"+goodsName);
            System.out.println("------------"+djia);
            System.out.println("------------"+dshu);
            System.out.println("------------"+Lingroot);
            purchaseService.getPurchasemxadd(cgLingcode,goodsName,Lingroot,djia, dshu,zmoney);
            purchase.setId(id);
            purchase.setCgOrder(1);
            purchaseService.update(purchase);
            return ResultVo.success("添加合同明细成功");
        }catch (Exception e){
            return ResultVo.fail("添加合同明细失败");
        }
    }
    @PostMapping("delpur")
    @RequiresPermissions("purchaser:del")
    public ResultVo getDelPurchaashe(@RequestBody Purchase purchase){
        try {
            purchaseService.getDelpur(purchase);
            purchaseService.del(purchase.getId());
            return ResultVo.success("添加回收站成功");
        }catch (Exception e){
            return ResultVo.fail("添加回收站失败",e);
        }
    }


}
